package cn.iocoder.yudao.module.blog.controller.app.ask;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.controller.admin.ask.vo.BlogAskCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.ask.vo.BlogAskPageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.ask.vo.BlogAskRespVO;
import cn.iocoder.yudao.module.blog.controller.admin.asklikeitem.vo.BlogAskLikeItemCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.asklikeitem.vo.BlogAskLikeItemRespVO;
import cn.iocoder.yudao.module.blog.controller.admin.askresponse.vo.BlogAskResponseCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.app.ask.vo.AppBlogAskRespVO;
import cn.iocoder.yudao.module.blog.convert.ask.BlogAskConvert;
import cn.iocoder.yudao.module.blog.convert.asklikeitem.BlogAskLikeItemConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.ask.BlogAskDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.asklikeitem.BlogAskLikeItemDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.askresponse.BlogAskResponseDO;
import cn.iocoder.yudao.module.blog.service.ask.BlogAskService;
import cn.iocoder.yudao.module.blog.service.asklikeitem.BlogAskLikeItemService;
import cn.iocoder.yudao.module.blog.service.askresponse.BlogAskResponseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 问答专区")
@RestController
@RequestMapping("/blog/ask")
@Validated
@Slf4j
public class AppBlogAskController {
    @Resource
    private BlogAskService askService;

    @Resource
    private BlogAskResponseService askResponseService;

    @Resource
    private BlogAskLikeItemService askLikeItemService;

    @GetMapping("/page")
    @Operation(summary = "获得学员提问分页")
    public CommonResult<PageResult<AppBlogAskRespVO>> getAskPage(@Valid BlogAskPageReqVO pageVO) {
        PageResult<BlogAskDO> pageResult = askService.getAskPage(pageVO);

        List<Long> askIds = CollectionUtils.convertList(pageResult.getList(), BlogAskDO::getId);

        List<BlogAskResponseDO> responseDOList = askResponseService.getAskResponseListByAskIds(askIds);
        Map<Long, List<BlogAskResponseDO>> responseMap = new HashMap<>();
        for(int i = 0; i<responseDOList.size(); i++){
            BlogAskResponseDO responseDO = responseDOList.get(i);
            if(!responseMap.containsKey(responseDO.getAskId())){
                responseMap.put(responseDO.getAskId(), new ArrayList<>());
            }

            responseMap.get(responseDO.getAskId()).add(responseDO);
        }

        return success(BlogAskConvert.INSTANCE.convertAppPage(pageResult, responseMap, null));
    }

    @PostMapping("/create")
    @Operation(summary = "创建学员提问")
    public CommonResult<Long> createAsk(@Valid @RequestBody BlogAskCreateReqVO createReqVO) {
        return success(askService.createAsk(createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得学员提问")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppBlogAskRespVO> getAsk(@RequestParam("id") Long id) {
        BlogAskDO ask = askService.getAsk(id);
        askService.readAsk(ask);

        List<BlogAskResponseDO> responseList = askResponseService.getAskResponseListByAskId(ask.getId());
        List<BlogAskLikeItemDO> likeItemList = askLikeItemService.getAskLikeItemListByAskId(ask.getId());

        return success(BlogAskConvert.INSTANCE.convertApp(ask, responseList, likeItemList));
    }

    @PostMapping("/response/create")
    @Operation(summary = "创建学员问题回复")
    public CommonResult<Long> createAskResponse(@Valid @RequestBody BlogAskResponseCreateReqVO createReqVO) {
        return success(askResponseService.createAskResponse(createReqVO));
    }

    @PostMapping("/like/create")
    @Operation(summary = "创建学员问题点赞")
    public CommonResult<Long> createAskLikeItem(@Valid @RequestBody BlogAskLikeItemCreateReqVO createReqVO) {
        return success(askLikeItemService.createAskLikeItem(createReqVO));
    }

    @GetMapping("/like/check")
    @Operation(summary = "获得学员问题点赞")
    @Parameter(name = "askId", description = "编号", required = true, example = "1024")
    public CommonResult<Long> getAskLikeItem(@RequestParam("askId") Long askId) {
        BlogAskLikeItemDO askLikeItem = askLikeItemService.getAskLikeItemByAskId(askId);

        if(askLikeItem == null){
            return success(-1L);
        }

        return success(askLikeItem.getId());
    }

}
